<?php


namespace app\admin\model;


use app\common\lib\RespCode;
use app\common\model\BaseModel;
use custom\Err;

class Notice extends BaseModel {

    /**
     * 列表
     * @param $params
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getList($params) {
        $count = self::count();
        if (empty($count)) {
            $result = [
                'page'    => 1,
                'pageNum' => 1,
                'count'   => 0,
                'data'    => [],
            ];
        } else {
            $page = empty($params['page']) ? 1 : intval($params['page']);
            $long = empty($params['long']) ? 10 : intval($params['long']);
            $data = self::page($page, $long)
                ->field('title,id,itime,status')
                ->order('sort', 'asc')
                ->select()
                ->toArray();
            foreach ($data as $k => $v) {
                $data[$k]['itime'] = date('Y-m-d H:i:s',$v['itime']);
            }
            $pageNum = ceil($count / $long);
            $result = [
                'page'    => $page,
                'pageNum' => $pageNum,
                'count'   => $count,
                'data'    => $data,
            ];
        }
        return $result;
    }

    /**
     * 编辑、添加
     * @param $params
     * @return bool
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public static function post($params) {
        if(empty(trim ($params['title']))){
            Err::add('不能为空', RespCode::PARAMS_ERR);
            return false;
        }
        if (mb_strlen(trim($params['title']), 'utf-8') > 30) {
            Err::add('长度限制30', RespCode::PARAMS_ERR);
            return false;
        }
        if (empty($params['id'])) {
            if (self::count() >= 10) {
                Err::add('最多添加十条', RespCode::PARAMS_ERR);
                return false;
            }
        }
        $data = [
            'title'   => trim($params['title']),
            'content' => '',
        ];
        if (empty($params['id'])) {
            $data['itime'] = time();
            self::insert($data);
        } else {
            self::where('id', $params['id'])->update($data);
        }
        return true;
    }

    /**
     * 删除
     * @param $params
     * @return bool
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public static function del($params) {
        if (!self::where('id', $params['id'])->select()->toArray()) {
            Err::add('数据不存在', RespCode::PARAMS_ERR);
            return false;
        }
        self::where('id', $params['id'])->delete();
        return true;
    }

    /**
     * 排序
     * @param $params
     * @throws \Exception
     */
    public static function editSort($params) {
        $ids = explode(',', $params['ids']);
        if ($ids) {
            $count = count($ids);
            $int = 1;
            for ($i = 0; $i < $count; $i++) {
                $arr = [
                    'id'   => $ids[$i],
                    'sort' => $int
                ];
                $list[] = $arr;
                $int++;
            }
            $region = new self();
            $region->saveAll($list);
        }
    }

    /**
     * 状态
     * @param $params
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public static function status($params){
        if(self::where ('id',$params['id'])->value ("status")==1){
            return self::where ('id',$params['id'])->update (['status'=>0]);
        }else{
            return self::where ('id',$params['id'])->update (['status'=>1]);
        }
    }

    /**
     * 详情
     * @param $id
     * @param $_info
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function info($id,&$_info){
        $info = self::where('id', $id)
            ->field('title')
            ->select()
            ->toArray ();
        if(empty($info)){
            Err::add('数据不存在', RespCode::PARAMS_ERR);
            return false;
        }
        $_info=$info[0];
        return true;
    }
}
